<template>
  <h1>员工列表综合练习</h1>
  <el-form style="width: 600px;margin: 0 auto">
    <el-form-item label="姓名">
      <el-input placeholder="请输入姓名" v-model="emp.name"></el-input>
    </el-form-item>
    <el-form-item label="工资">
      <el-input placeholder="请输入工资" v-model="emp.salary"></el-input>
    </el-form-item>
    <el-form-item label="岗位">
      <el-input placeholder="请输入岗位" v-model="emp.job"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="commit">点我提交</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="arr" style="width: 600px;margin: 0 auto">
    <el-table-column type="index" label="序号" width="80" align="center"/>
    <el-table-column prop="name" label="姓名"/>
    <el-table-column prop="salary" label="工资"/>
    <el-table-column prop="job" label="岗位"/>
    <el-table-column label="操作" align="center">
      <template #default="scope">
        <el-button type="danger" @click="del(scope.row,scope.$index)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
import {ref} from "vue";
import {ElMessage} from "element-plus";

const emp = ref({name: '', salary: '', job: ''})
const arr = ref([]);
const commit = () => {
  if (emp.value.name.trim()=='' || emp.value.salary.trim()=='' || emp.value.job.trim()=='') {
    ElMessage.warning('请输入员工信息!');
    return;
  }
  arr.value.push(emp.value);
  emp.value = {name: '', salary: '', job: ''};
  console.log(arr.value);
};
const del = (emp, index) => {
  if (confirm('您确定要删除' + emp.name + '吗?')) {
    arr.value.splice(index, 1);
  }
}
</script>

<style scoped>

</style>